package com.itboye.lottery.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.bylibrary.util.TimeUtils;
import com.itboye.lottery.R;
import com.itboye.lottery.bean.JinEDetail;

import java.util.ArrayList;

/**
 * Created by wu on 2018/1/9.
 */

public class MingXiAdapter extends RecyclerView.Adapter<MingXiAdapter.ViewHolderMingXi> {

    private Context context;
    ArrayList<JinEDetail> classEntityArrayList;
    Activity activity;

    public MingXiAdapter(Activity activity, ArrayList<JinEDetail> classEntityArrayList) {
        this.activity = activity;
        this.classEntityArrayList = classEntityArrayList;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(MingXiAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public ViewHolderMingXi onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolderMingXi(LayoutInflater.from(context).inflate(R.layout.item_mingxi, parent, false));
    }


    @Override
    public void onBindViewHolder(ViewHolderMingXi holder, int position) {
        JinEDetail jinEDetail = classEntityArrayList.get(position);
        holder.tv_1.setText(jinEDetail.getDesc());
        if (jinEDetail.getAdd_time() != null) {
            holder.tv_2.setText(TimeUtils.getFormatTime(Long.parseLong(jinEDetail.getAdd_time()) * 1000, "yyyy-MM-dd HH:mm"));
        }
        holder.tv_3.setText(jinEDetail.getUser_money() + "元");
        holder.tv_4.setText(jinEDetail.getStatus());
        if (jinEDetail.getType() != null) {
            switch (Integer.valueOf(jinEDetail.getType())) {
                case 3:
                    holder.tv_3.setText(jinEDetail.getRecharge_money() + "元");
                    holder.tv_1.setText("充值");
                    holder.tv_4.setVisibility(View.GONE);
                    holder.iv_1.setBackgroundResource(R.mipmap.chongzhi);
                    break;
                case 4:
                    holder.tv_4.setVisibility(View.VISIBLE);
                    holder.tv_4.setText(jinEDetail.getRemark());
                    holder.iv_1.setBackgroundResource(R.mipmap.tixian);
                    break;
                default:
                    holder.tv_4.setVisibility(View.GONE);
                    holder.iv_1.setBackgroundResource(R.mipmap.zhanghumingxi);
                    break;
            }
        }
//        holder.tv_5.setText(jinEDetail.getDesc());
    }

    @Override
    public int getItemCount() {
        return classEntityArrayList == null ? 0 : classEntityArrayList.size();
    }


    public class ViewHolderMingXi extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView tv_1, tv_2, tv_3, tv_4, tv_5;
        ImageView iv_1;

        public ViewHolderMingXi(View itemView) {
            super(itemView);
            tv_1 = itemView.findViewById(R.id.tv_1);
            tv_2 = itemView.findViewById(R.id.tv_2);
            tv_3 = itemView.findViewById(R.id.tv_3);
            tv_4 = itemView.findViewById(R.id.tv_4);
            tv_5 = itemView.findViewById(R.id.tv_5);
            iv_1 = itemView.findViewById(R.id.iv_1);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

}
